package com.opengms.HydroModelsIntegration.Controller;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Author: David.Xiao
 * @Date: 2018/9/29 16:31
 * @Description:
 */

@WebServlet(name="ModelDataDownloadServlet",urlPatterns = "/ModelDataDownloadServlet")
public class ModelDataDownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置对客户端请求进行重新编码的编码
        req.setCharacterEncoding("utf-8");
        //使客户端浏览器，区分不同种类的数据，并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。
//        resp.setContentType("text/html;charset=UTF-8;pageEncoding=UTF-8");
        //指定对服务器响应进行重新编码的编码
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("multipart/form-data");
        System.out.println(System.getProperty("java.class.path"));

        //获取向前台写数据的输出流
        ServletOutputStream out= null;
        FileInputStream inputStream= null;

        try {
            out = resp.getOutputStream();

//            String localFileName=req.getParameter("dataID")+".inp";
//            ResourceBundle resourceBundle = ResourceBundle.getBundle("resources/modelData");
//            resourceBundle.getString(localFileName);

            Properties properties = new Properties();

            System.out.println(ModelDataDownloadServlet.class.getClassLoader());
            InputStream inputStream1 = ModelDataDownloadServlet.class.getClassLoader().getResourceAsStream("modelData.properties");
            properties.load(inputStream1);
            String localFileName = properties.getProperty(req.getParameter("dataID"));



            String localFilePath=this.getClass().getResource("/").getPath()+"\\modelData";
            String localFileFullPath=localFilePath+"\\"+localFileName;
            resp.setHeader("Content-Disposition", "attachment; filename=" + localFileName);

            File file=new File(localFileFullPath);
            if(!file.exists())
            {
                return;
            }
            inputStream = new FileInputStream(file);


            int len=0;
            byte[] buffer = new byte[1024 * 10];
            while ((len = inputStream.read(buffer)) != -1){
                out.write(buffer,0,len);
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                inputStream.close();
            } catch (IOException e) {
                System.out.println("关闭流异常");
                e.printStackTrace();
            }
        }




    }
}
